<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\mill\model\Order as OrderModel;
use app\mill\model\Give as GiveModel;
use app\mill\model\Sample as SampleModel;
use app\mill\model\Finance as FinanceModel;
use think\Db;

/**
 * 送货表模型
 */
class Address extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_order_archives_address';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //获取扫码人最近的发货单号
    public static function lastAddressSn($uid = '',$give = []){

        $status = GiveModel::$role[8]['status'][0];
        $where = [
            ['status','=',$status],
            ['address_status','=',0],
            ['car_id','=',$give['car_id']],
            ['order_id','=',$give['order_id']],
            ['status_flow','like',"%\\_{$status}|{$uid}\\_%"],
        ];
        $address_sn = Db::name('mill_order_archives_give')->where($where)->order("status{$status}_time desc")->value('address_sn');

        return $address_sn;
    }
    // 表格制作
    //print 0:编辑模式 1:打印模式 2:导出模式
    public static function fromTabale($data = [],$table_give = [],$print = 0){

        $table_head = [];

        $table_data[] = [
            "[14:1]<div style='text-align:center;font-size:24px;letter-spacing: 20px;' class='td-none td-bottom'>
                铝板发运单
                ".SampleModel::inputToText('id',$data['id'],$print,'','input','hidden')
            .SampleModel::inputToText('order_pid',$data['order_pid'],$print,'','input','hidden')
            .SampleModel::inputToText('order_ids',$data['order_ids'],$print,'','input','hidden')
            .SampleModel::inputToText('archives_sns',$data['archives_sns'],$print,'','input','hidden')
            .SampleModel::inputToText('create_time',$data['create_time'],$print,'','input','hidden')."
            </div>",
        ];
        $table_data[] = [
            "[5:1]<div style='text-align:left;font-size:14px;' class='td-none td-bottom td-35 font-w-400'>工程名称：{$data['engineering']}</div>",
            "[2:1]<div style='text-align:left;font-size:14px;' class='td-none td-bottom td-35 font-w-400'>工程批次：{$data['batch']}</div>",
            "[3:1]<div style='text-align:left;font-size:14px;' class='td-none td-bottom td-35 font-w-400'>订单编号：{$data['order_sn']}</div>",
            "[4:1]<div style='text-align:left;font-size:14px;' class='td-none td-bottom td-35 font-w-400'>送货单号：
                ".SampleModel::inputToText('address_sn',$data['address_sn'],$print,'','input','hidden')."
                {$data['address_sn']}
            </div>",
        ];

        $give_head = [];
        $give_head_span = [];
        foreach ($table_give['head'] as $v){
            $give_head[] = "<div class='td-border' style='font-weight: bold;text-align:center;'>{$v}</div>";
            $give_head_span[] = OrderModel::tableParseCell($v);
        }
        $table_data[] = $give_head;

        if($print){

            //$table_head = $table_data;
            //$table_data = [];
        }

        $give_list = [];

        $i = count($table_give['data'])-1;

        //合计
        $all_number_sum = 0;
        $all_area_sum = 0;
        //$all_area2_sum = 0;

        $all_weld_sum = 0;//焊缝
        $all_slot_sum = 0;//刨槽
        $all_bend_sum = 0;//超长
        $all_torque_sum = 0;//超宽
        $all_extra_sum = 0;//配件
        $all_price_sum = 0;//总价

        foreach ($table_give['data'] as $gk=>$gv){

            //面积表ID
            $archives_id = $gv['archives_id'];
            //显示单价 0:不显示 1:显示
            if(!empty($data['is_price']) && isset($gv['price'])){
                $price_arr = FinanceModel::getPriceInfo($archives_id);
                if(!empty($price_arr['price'])){
                    $gv['price'] = $price_arr['price'];
                    $gv['price_sum'] = round(($price_arr['price']*$gv['row_area']),2);
                }
            }
            //合计
            $all_number_sum += $gv['number'];
            $all_area_sum += $gv['row_area'];
            //$all_area2_sum += $gv['row_area2'];

            if(!empty($gv['weld']))$all_weld_sum += floatval($gv['weld']);//焊缝
            if(!empty($gv['slot']))$all_slot_sum += floatval($gv['slot']);//刨槽
            if(!empty($gv['bend']))$all_bend_sum += floatval($gv['bend']);//超长
            if(!empty($gv['torque']))$all_torque_sum += floatval($gv['torque']);//超宽
            if(!empty($gv['extra']))$all_extra_sum += floatval($gv['extra']);//配件
            if(!empty($gv['price_sum']))$all_price_sum += floatval($gv['price_sum']);//总价

            $give = [];

            unset($gv['id']);
            unset($gv['archives_id']);
            foreach ($gv as $k=>$v){
                if(isset($table_give['data_span'][$k]))$v = "{$table_give['data_span'][$k]}{$v}";
                $give[] = "<div class='td-border' style='text-align:center;'>{$v}</div>";
            }
            $give_list[] = $give;

            //最后一条记录
            if($i == $gk){

                //合计
                $gv_new = [];

                $gv_new[] = "[{$give_head_span[0]['colspan']}:{$give_head_span[0]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'>合计：</div>";
                $gv_new[] = "[{$give_head_span[1]['colspan']}:{$give_head_span[1]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[2]['colspan']}:{$give_head_span[2]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'>{$all_number_sum}</div>";
                $gv_new[] = "[{$give_head_span[3]['colspan']}:{$give_head_span[3]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[4]['colspan']}:{$give_head_span[3]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[5]['colspan']}:{$give_head_span[3]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'>{$all_area_sum}</div>";
                $gv_new[] = "[{$give_head_span[6]['colspan']}:{$give_head_span[4]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[7]['colspan']}:{$give_head_span[5]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'>{$all_weld_sum}</div>";
                $gv_new[] = "[{$give_head_span[8]['colspan']}:{$give_head_span[6]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[9]['colspan']}:{$give_head_span[7]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'>{$all_slot_sum}</div>";
                $gv_new[] = "[{$give_head_span[10]['colspan']}:{$give_head_span[8]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[11]['colspan']}:{$give_head_span[9]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[12]['colspan']}:{$give_head_span[10]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                $gv_new[] = "[{$give_head_span[13]['colspan']}:{$give_head_span[11]['rowspan']}]<div class='td-border' style='font-weight: bold;text-align:center;'></div>";

                if(isset($gv['price']))$gv_new[] = "<div class='td-border' style='font-weight: bold;text-align:center;'></div>";
                if(isset($gv['price_sum']))$gv_new[] = "<div class='td-border' style='font-weight: bold;text-align:center;'>{$all_price_sum}</div>";

                $give_list[] = $gv_new;

            }
        }

        $table_data = array_merge_recursive($table_data,$give_list);

        //产品分组合计
        $table_group_head = [
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
            "[1:".(count($table_give['group'])+2)."]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>分类统计</div>",
            "[4:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>板型</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>板厚</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>刀数</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>数量</div>",
            "[2:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>面积</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>焊缝</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>开槽</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
        ];
        $table_data[] = $table_group_head;

        $group_arr = [];
        foreach ($table_give['group'] as $ggk=>$ggv){

            //if(in_array($ggv['plank'].$ggv['color'],$group_arr))$ggv['plank'] = $ggv['color'] = '';
            //else $group_arr[] = $ggv['plank'].$ggv['color'];

            $table_group_data = [
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
                "[4:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['model']}</div>",
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['thickness']}</div>",
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['knife']}</div>",
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['number_sum']}</div>",
                "[2:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['area_sum']}</div>",
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['weld_sum']}</div>",
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$ggv['slot_sum']}</div>",
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
            ];

            $table_data[] = $table_group_data;
        }

        //总计
        $table_group_data = [
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
            "[4:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>总计</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$all_number_sum}</div>",
            "[2:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$all_area_sum}</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$all_weld_sum}</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>{$all_slot_sum}</div>",
            "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'></div>",
        ];

        $table_data[] = $table_group_data;

        //$table_data[] = [
        //    "[14:1]<div style='text-align: left;font-weight: bold;' class='scale-left td-none td-top td-50'>注：</div>",
        //];
        //$table_data[] = [
        //    "[14:1]<div style='text-align: left;font-weight: bold;' class='scale-left td-none td-top td-50'>".SampleModel::inputToText('explain',$data['explain'],$print,'height: 150px;','textarea')."</div>",
        //];

        if(empty($data['extra']) && !empty($table_give['extra'])){
            foreach ($table_give['extra'] as $extra){
                $data['extra'] .= "{$extra['model_sn']}：{$extra['number']}\r\n";
            }
        }
        if(!empty($data['extra'])){
            $table_data[] = [
                "[1:1]<div style='text-align: center;font-weight: bold;' class='td-border scale-left'>配件</div>",
                "[13:1]<div style='text-align: left;font-weight: bold;' class='td-border scale-left'>".SampleModel::inputToText('extra',$data['extra'],$print,'height: 150px;','textarea')."</div>",
            ];
        }

        $table_data[] = [
            "[14:1]<div style='text-align: left;font-weight: bold;' class='scale-left td-none td-top td-50'>".SampleModel::inputToText('note',$data['note'],$print,'height: 150px;','textarea')."</div>",
        ];

        $table_data[] = [
            "[6:1]<div style='text-align:left;' class='td-none td-top td-50 font-w-400'>驾驶员证件号：
                ".SampleModel::inputToText('car_cart',$data['car_cart'],$print,'width: 50%;display: inline;')."
            </div>",
            "[4:1]<div style='text-align:left;' class='td-none td-50 font-w-400'>驾驶员电话：
                ".SampleModel::inputToText('car_mobile',$data['car_mobile'],$print,'width: 50%;display: inline;')."
            </div>",
            "[4:1]<div style='text-align:left;' class='td-none td-50 font-w-400'>驾驶员签字：</div>",
        ];

        $table_data[] = [
            "[14:1]<div style='text-align:left;' class='td-none td-50 font-w-400'>发货人签字：
                ".SampleModel::inputToText('user_nickname',$data['user_nickname'],$print,'width: 50%;display: inline;')."
            </div>",
        ];

        $table_data[] = [
            "[6:1]<div style='text-align:left;' class='td-none td-top td-50 font-w-400'>收货地址：
                ".SampleModel::inputToText('address',$data['address'],$print,'width: 50%;display: inline;')."
            </div>",
            "[4:1]<div style='text-align:left;' class='td-none td-50 font-w-400'>收货人电话：
                ".SampleModel::inputToText('order_nickname1',$data['order_nickname1'],$print,'width: 50%;display: inline;')."
            </div>",
            "[4:1]<div style='text-align:left;' class='td-none td-50 font-w-400'>收货人签字：</div>",
        ];

        $table_data[] = [
            "[8:1]<div style='text-align:left;' class='td-none td-top td-50 font-w-400'></div>",
            "[6:1]<div style='text-align:left;' class='td-none td-top td-50 font-w-400'>送货日期：
                ".SampleModel::inputToText('address_date',$data['address_date'],$print,'','input','hidden')."
                {$data['address_date']}
            </div>",
        ];


        return ['head'=>$table_head,'data'=>$table_data,'give'=>['head'=>$give_head,'data'=>$give_list]];
    }

    // 打印模板
    //print 1:打印模式 2:导出模式
    public static function toHtml($data = [],$table = [],$print = 1){

        //缩放比例
        $table_scaling = 1;

        $html = '<html><head><title>'.config('web_site_title').'</title></head><style>th{font-size: 12px;font-weight: bold;}td{font-size: 10px;}</style><body style="margin: 4px;">';

        //$head_img = url_path(change_path(config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo-excel.jpg'));
        $head_img = config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo-excel.jpg';
        //头部
        //$html .= '<table border="1" style="text-align: center;border-collapse: collapse;width: '.(21/$table_scaling).'cm;transform-origin: 0 0;"><thead>
        $html .= '<table border="1" style="text-align: center;border-collapse: collapse;transform-origin: 0 0;"><thead>
                        <tr><th colspan="14" style="font-size: 25px;font-weight: bold;text-align:center;"><img style="width: 170px;margin-bottom: -22px;" class="td-none" src="'.config('template.tpl_replace_string')['__ADMIN_IMG__'].'/logo2.jpg">'.config('web_site_title').'</th></tr>';
        //$html .= '<table border="1" style="text-align: center;border-collapse: collapse;transform-origin: 0 0;"><thead>';
        $table_arr = self::fromTabale($data,$table,1);
        foreach ($table_arr as $tk=>$tv){
            //头
            if($tk == 'head'){
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = OrderModel::tableParseCell($tvvv);

                        $html .= '
                            <th rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</th>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</thead>';
            }
            //体
            elseif($tk == 'data'){
                $html .= '<tbody>';
                foreach ($tv as $tvv){

                    $html .= '<tr>';
                    foreach ($tvv as $tvvv){

                        $v = OrderModel::tableParseCell($tvvv);

                        //$v['value'] = str_replace("\n","<br>",$v['value']);
                        $html .= '
                            <td rowspan="'.$v['rowspan'].'" colspan="'.$v['colspan'].'">'.$v['value'].'</td>
                            ';
                    }
                    $html .= '</tr>';
                }
                $html .= '</tbody>';
            }
        }
        $html .= '</table>';

        $js = 'window.print();';
        if($print == 2){
            $js = '$("table").table2excel({
                            // 不被导出的表格行的CSS class类
                            exclude: "",
                            // 导出的Excel文档的名称
                            name: "Excel Document Name",
                            // Excel文件的名称
                            filename: "发货单",
                            //文件后缀名
                            fileext: ".xls",
                            //是否排除导出图片
                            exclude_img: false,
                            //是否排除导出超链接
                            exclude_links: true,
                            //是否排除导出输入框中的内容
                            exclude_inputs: false
                        });';
        }
        //js
        $html .= '
                    <script src="'.config('template.tpl_replace_string')['__ADMIN_JS__'].'/core/jquery.min.js?v=20180327"></script>
                    <script src="'.config('template.tpl_replace_string')['__STATIC__'].'/common/js/jquery.table2excel.js?v='.date('Ymd').'"></script>
                    <script type="text/javascript">
                        //设置边框
                        //$(".td-border").parent().css("border","1px solid #777");
                        //去除边框
                        $(".td-none").parent().css("border-style","hidden")
                        //设置头部边框
                        $(".td-top").parent().css("border-top","black");
                        //设置底部边框
                        $(".td-bottom").parent().css("border-bottom","black");
                        //设置高度
                        $(".td-35").parent().css("height","35px");
                        $(".td-50").parent().css("height","50px");
                        //设置向下对齐
                        $(".text-bottom").parent().css("vertical-align","bottom");
                        //设置文字上下间距
                        $(".text-height").parent().css("line-height","28px");
                        $(".font-w-400").parent().css("font-weight","400");
                        //设置文字缩放比例
                        $("th,td").css("-webkit-transform","scale('.$table_scaling.')");
                        //设置文字缩放后左对齐
                        $(".scale-left").parent().css("transform-origin","0");
                        
                        '.$js.'
                    </script>';

        return $html;
    }

}
